import { Ref, ComputedRef, watch } from 'vue'
import type { VxeGridInstance } from "vxe-table";
import { useKeyboard } from '/@/hooks/useKeyboard'

const keyboardState = useKeyboard();

export function useCheckRange({
  params, vxeGridProps, vxeGridRef, getProps
}) {

  const contrlSelect = keyboardState.ctrl || keyboardState.shift

  if (contrlSelect) {

    // 控制选择行
    const tableData = vxeGridProps.value.data

    if (keyboardState.ctrl) {
      vxeGridRef.value.setCheckboxRow(params.row, true)
    } else if (keyboardState.shift) {

      const currRowIndex = params.rowIndex

      const indexes = vxeGridRef.value
        .getCheckboxRecords(true)
        .map(r => tableData.findIndex(c => c === r))
        .filter(s => s > -1)

      let maxAbsIndex = 0;
      indexes.forEach(i => {
        const abs = Math.abs(currRowIndex - i)
        if (abs > maxAbsIndex) {
          maxAbsIndex = i
        }
      })

      if (currRowIndex > maxAbsIndex) {
        for (let i = maxAbsIndex; i <= currRowIndex; i++) {
          vxeGridRef.value.setCheckboxRow(tableData[i], true)
        }
      } else {
        for (let i = currRowIndex; i <= maxAbsIndex; i++) {
          vxeGridRef.value.setCheckboxRow(tableData[i], true)
        }
      }
    }
  } else {
    vxeGridProps.value.data.forEach(row => {
      vxeGridRef.value.setCheckboxRow(row, row === params.row)
    })
  }
}